import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

/**
 * The main <code>JFrame</code> for the application.
 */
public class View extends JFrame implements ActionListener{
	
	private TablePanel panel;
	private Model parent;
	
	private JMenuBar menuBar;
	private JMenu fileMenu, cardMenu, tableMenu, colourSubMenu;
	private JMenuItem fQuitMenuItem;
	private JMenuItem cFlipMenuItem;
	private JRadioButtonMenuItem tcGreenTableRadio, tcRedTableRadio, tcBlueTableRadio;
	
	/**
	 * <ul><li>Constructs the <code>JFrame</code>.</li>
	 * <li>Connects to the {@link Model}.</li>
	 * <li>Creates the menu.</li></ul>
	 * 
	 * @param parent The <code>Model</code> to connect to.
	 */
	public View(Model parent) {
		
		super("Super Awesome Cards Win");
		this.parent = parent;
		
		menuBar = new JMenuBar();
		fileMenu = new JMenu("File");
		fileMenu.setMnemonic(KeyEvent.VK_F);
		menuBar.add(fileMenu);
			fQuitMenuItem = new JMenuItem("Quit", KeyEvent.VK_Q);
				KeyStroke ctrlQKeyStroke = KeyStroke.getKeyStroke("control Q");
				fQuitMenuItem.setAccelerator(ctrlQKeyStroke);
				fileMenu.add(fQuitMenuItem);
				fQuitMenuItem.addActionListener(this);
				
		cardMenu = new JMenu("Card");
		cardMenu.setMnemonic(KeyEvent.VK_C);
		menuBar.add(cardMenu);
			cFlipMenuItem = new JMenuItem("Flip", KeyEvent.VK_F);
				KeyStroke fKeyStroke = KeyStroke.getKeyStroke("F");
				cFlipMenuItem.setAccelerator(fKeyStroke);
				cardMenu.add(cFlipMenuItem);
				cFlipMenuItem.addActionListener(this);
			
		tableMenu = new JMenu("Table");
		tableMenu.setMnemonic(KeyEvent.VK_T);
		menuBar.add(tableMenu);
			colourSubMenu = new JMenu("Table Colour");
			tableMenu.add(colourSubMenu);
				ButtonGroup colour = new ButtonGroup();
				tcGreenTableRadio = new JRadioButtonMenuItem("Green");
				tcRedTableRadio = new JRadioButtonMenuItem("Red");
				tcBlueTableRadio = new JRadioButtonMenuItem("Blue");
				colourSubMenu.add(tcGreenTableRadio);
				colourSubMenu.add(tcRedTableRadio);
				colourSubMenu.add(tcBlueTableRadio);
				colour.add(tcGreenTableRadio);
				colour.add(tcRedTableRadio);
				colour.add(tcBlueTableRadio);
				tcGreenTableRadio.setSelected(true);
				tcGreenTableRadio.addActionListener(this);
				tcRedTableRadio.addActionListener(this);
				tcBlueTableRadio.addActionListener(this);
				
		this.setJMenuBar(menuBar);
		
		
		panel = new TablePanel(parent);
		this.setContentPane(panel);
		repaint();
		
		this.addWindowListener(new WL());
		this.setPreferredSize(new Dimension(800,600));
		this.pack();
		this.setVisible(true);		
	}
	
	/**
	 * Sets the {@link Model} that this frame is connected to.
	 * 
	 * @param m The model to set.
	 */
	public void setModel(Model m) {
		panel.setModel(m);
	}
	
	/**
	 * The <code>WindowAdapter</code> that listens for <code>windowClosing</code> events.
	 */
	public class WL extends WindowAdapter {
		public void windowClosing(WindowEvent e) {
			System.exit(0);
		}
	}
	
	/**
	 * Processes mouse events for the menus.
	 */
	public void actionPerformed(ActionEvent ae){
		
		Object source = ae.getSource();
		
		if(source == fQuitMenuItem){
			//close
			System.exit(0);
			
		} else if(source == cFlipMenuItem){
			parent.flipSelected();
			
		} else if(source == tcGreenTableRadio) {
			System.out.println("Change table colour to - green");
			panel.changeBackgroundColour("green");
			
		} else if(source == tcRedTableRadio) {
			System.out.println("Change table colour to - red");
			panel.changeBackgroundColour("red");
		} else if(source == tcBlueTableRadio) {
			System.out.println("Change table colour to - blue");
			panel.changeBackgroundColour("blue");
		}
	}
}
